﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityMapMgr.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-16
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityMapMgr
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityMapMgr.h"

#include "NFCityMapInst.h"
#include "DescStore/CityMapDesc.h"

NFCityMapMgr::NFCityMapMgr()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFCityMapMgr::~NFCityMapMgr()
{
}

int NFCityMapMgr::CreateInit()
{
    m_bInited = false;
    return 0;
}

int NFCityMapMgr::ResumeInit()
{
    return 0;
}

int NFCityMapMgr::Init()
{
    if (m_bInited)
        return 0;
    for (int i = 0; i < (int)CityMapDesc::Instance()->GetResNum(); ++i)
    {
        auto pCityMapDesc = CityMapDesc::Instance()->GetDescByIndex(i);
        CHECK_NULL(0, pCityMapDesc);
        if (pCityMapDesc->map_type != EN_MAP_TYPE_CITY && pCityMapDesc->map_type != EN_MAP_TYPE_OUTDOOR && pCityMapDesc->map_type != EN_MAP_TYPE_SIX_EXPERIMENT && pCityMapDesc->map_type != EN_MAP_TYPE_TRAPTOWER && pCityMapDesc->map_type != EN_MAP_TYPE_SKYARENA_BATTLE && pCityMapDesc->map_type !=
            EN_MAP_TYPE_WHEEL_WAR_OUTDOOR && pCityMapDesc->map_type != EN_MAP_TYPE_RAID_TEAM && pCityMapDesc->map_type != EN_MAP_TYPE_GET_MONEY)
        {
            continue;
        }

        NFCityMapInst* pMapInst = CreateStaticMap(pCityMapDesc->id);
        CHECK_NULL_WF(0, pMapInst, "CreateStaticMap failed, mapDescId:{}", pCityMapDesc->id);
    }
    m_bInited = true;
    return 0;
}

NFCityMapInst* NFCityMapMgr::CreateStaticMap(int iMapDescId)
{
    auto pDesc = CityMapDesc::Instance()->GetDesc(iMapDescId);
    CHECK_NULL_RE_NULL(iMapDescId, pDesc, "GetDesc failed, mapDescId:{}", iMapDescId);

    if (m_aiStaticMapInst.full())
    {
        LOG_ERR(iMapDescId, -1, "static map inst full, mapDescId:{}", iMapDescId);
        return nullptr;
    }

    NFCityMapInst* pMapInst = NFCityMapInst::CreateObj();
    CHECK_NULL_RE_NULL(iMapDescId, pMapInst, "CreateObjByHashKey failed, mapDescId:{}", iMapDescId);

    int iRetCode = pMapInst->Init(iMapDescId);
    if (iRetCode != 0)
    {
        NFCityMapInst::DestroyObj(pMapInst);
        CHECK_ERR_RE_NULL(iMapDescId, iRetCode, "map inst init failed, mapDescId:{}", iMapDescId);
    }

    LOG_INFO(0, "create map inst:{} success!", iMapDescId);
    m_aiStaticMapInst[iMapDescId] = pMapInst->GetGlobalId();

    return pMapInst;
}

NFCityMapInst* NFCityMapMgr::GetStaticMap(int iMapDescId)
{
    NFCityMapInst* pMapInst = nullptr;
    auto iter = m_aiStaticMapInst.find(iMapDescId);
    if (iter != m_aiStaticMapInst.end())
    {
        pMapInst = NFCityMapInst::GetObjByGlobalId(iter->second);
        if (pMapInst)
        {
            return pMapInst;
        }
    }
    else
    {
        pMapInst = CreateStaticMap(iMapDescId);
        CHECK_NULL_RE_NULL(iMapDescId, pMapInst, "CreateStaticMap failed, mapDescId:{}", iMapDescId);
    }

    return pMapInst;
}

NFCityMapInst* NFCityMapMgr::GetMapInst(NFCityPlayer* pPlayer, int iMapDescId)
{
    auto pDesc = CityMapDesc::Instance()->GetDesc(iMapDescId);
    CHECK_NULL_RE_NULL(iMapDescId, pDesc, "GetDesc failed, mapDescId:{}", iMapDescId);

    switch (pDesc->map_type)
    {
        case EN_MAP_TYPE_GUILD_STATION:
            return nullptr;
        case EN_MAP_TYPE_GUILD_BATTLE:
            return nullptr;
        case EN_MAP_TYPE_FAMILY_STATION:
            return nullptr;
        case EN_MAP_TYPE_DUNGEON:
            return nullptr;
        case EN_MAP_TYPE_RAID_STANDBY:
            return nullptr;
        default:
            return GetStaticMap(iMapDescId);
    }

    return nullptr;
}

int NFCityMapMgr::DestroyMapInst(NFCityMapInst* pMapInst)
{
    CHECK_NULL(0, pMapInst);

    auto pCityMapDesc = CityMapDesc::Instance()->GetDesc(pMapInst->GetMapDescId());
    CHECK_NULL_WF(0, pCityMapDesc, "invalid map desc:{}", pMapInst->GetMapDescId());

    if (pCityMapDesc->map_type == proto_ff::EN_MAP_TYPE_GUILD_STATION)
    {
        pMapInst->Fin();
        NFCityMapInst::DestroyObj(pMapInst);
    }
    else if (pCityMapDesc->map_type == proto_ff::EN_MAP_TYPE_GUILD_BATTLE)
    {
        pMapInst->Fin();
        NFCityMapInst::DestroyObj(pMapInst);
    }
    else if (pCityMapDesc->map_type == proto_ff::EN_MAP_TYPE_FAMILY_STATION)
    {
        pMapInst->Fin();
        NFCityMapInst::DestroyObj(pMapInst);
    }
    else if (pCityMapDesc->map_type == proto_ff::EN_MAP_TYPE_DUNGEON)
    {
        pMapInst->Fin();
        NFCityMapInst::DestroyObj(pMapInst);
    }
    else if (pCityMapDesc->map_type == proto_ff::EN_MAP_TYPE_RAID_STANDBY)
    {
        pMapInst->Fin();
        NFCityMapInst::DestroyObj(pMapInst);
    }

    return 0;
}
